// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "pattern_name": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "maxLength": 100
      }
    ],
    "title": "图案名称",
    "label": "图案名称"
  },
  "pattern_name_en": {
    "rules": [
      {
        "format": "string"
      },
      {
        "maxLength": 100
      }
    ],
    "title": "图案英文名称",
    "label": "图案英文名称"
  },
  "pattern_file": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "图案文件路径",
    "label": "图案文件路径"
  },
  "preview_image": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "预览图片URL",
    "label": "预览图片URL"
  },
  "category": {
    "rules": [
      {
        "format": "string"
      },
      {
        "range": [
          {
            "value": "nature",
            "text": "nature"
          },
          {
            "value": "animal",
            "text": "animal"
          },
          {
            "value": "abstract",
            "text": "abstract"
          },
          {
            "value": "geometry",
            "text": "geometry"
          },
          {
            "value": "art",
            "text": "art"
          },
          {
            "value": "other",
            "text": "other"
          }
        ]
      }
    ],
    "title": "图案分类",
    "label": "图案分类"
  },
  "tags": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "标签列表",
    "label": "标签列表"
  },
  "duration": {
    "rules": [
      {
        "format": "int"
      },
      {
        "minimum": 0
      }
    ],
    "title": "播放时长（秒）",
    "label": "播放时长（秒）"
  },
  "difficulty": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "value": 1,
            "text": 1
          },
          {
            "value": 2,
            "text": 2
          },
          {
            "value": 3,
            "text": 3
          },
          {
            "value": 4,
            "text": 4
          },
          {
            "value": 5,
            "text": 5
          }
        ]
      }
    ],
    "title": "复杂度等级",
    "label": "复杂度等级"
  },
  "download_count": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "下载次数",
    "label": "下载次数"
  },
  "favorite_count": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "收藏次数",
    "label": "收藏次数"
  },
  "designer": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "设计师",
    "label": "设计师"
  },
  "description": {
    "rules": [
      {
        "format": "string"
      },
      {
        "maxLength": 500
      }
    ],
    "title": "图案描述",
    "label": "图案描述"
  },
  "is_featured": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "是否为精选图案",
    "label": "是否为精选图案"
  },
  "is_premium": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "是否为付费图案",
    "label": "是否为付费图案"
  },
  "compatible_devices": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "兼容设备类型",
    "label": "兼容设备类型"
  },
  "status": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "value": 0,
            "text": 0
          },
          {
            "value": 1,
            "text": 1
          },
          {
            "value": 2,
            "text": 2
          }
        ]
      }
    ],
    "title": "状态：0-草稿，1-发布，2-下架",
    "label": "状态：0-草稿，1-发布，2-下架"
  }
}

const enumConverter = {
  "category_valuetotext": {
    "nature": "nature",
    "animal": "animal",
    "abstract": "abstract",
    "geometry": "geometry",
    "art": "art",
    "other": "other"
  },
  "difficulty_valuetotext": {
    "1": 1,
    "2": 2,
    "3": 3,
    "4": 4,
    "5": 5
  },
  "status_valuetotext": {
    "0": 0,
    "1": 1,
    "2": 2
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
